<template>
  <div class="course-wrap">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-fa fa-book"></i> 课程管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="container">
      <div class="query-form">
        <el-row :gutter="20">
          <el-col :span="2">
            <el-button @click="create" icon="el-icon-plus">创建</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="table">
        <el-table :data="tableData" stripe>
          <el-table-column label="课程Id" prop="id" />
          <el-table-column label="课程名" prop="name" width="200px" />
          <el-table-column label="年级" prop="grade" />
          <el-table-column label="学分" prop="credit" />
          <el-table-column
            align="center"
            label="上课时间"
            prop="time"
            width="130px"
          />
          <el-table-column label="上课地点" prop="location" />
          <el-table-column label="已选人数" prop="selectedCount" />
          <el-table-column label="最大容量" prop="maxSize" />
          <el-table-column label="状态" prop="status" width="100">
            <template slot-scope="scope">
              <el-tag :type="getStatusType(scope.row.status)">
                {{ getStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="审核时间" prop="auditTime" width="160">
            <template slot-scope="scope">
              {{ formatDate(scope.row.auditTime) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">
              <el-button @click="edit(scope.row.id)" size="mini" type="success"
                >编辑
              </el-button>
              <el-button
                @click="deleteItem(scope.row.id)"
                size="mini"
                type="danger"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog :visible.sync="editing" title="编辑" width="30%">
        <el-form :model="entityForm" label-width="70px" ref="form">
          <el-form-item label="课程名">
            <el-input v-model="entityForm.name"></el-input>
          </el-form-item>
          <el-form-item label="年级">
            <el-input type="number" v-model="entityForm.grade"></el-input>
          </el-form-item>
          <el-form-item label="上课时间">
            <el-select v-model="courseDay">
              <el-option
                v-for="(item, index) in days"
                :key="index"
                :label="item"
                :value="index"
              >
              </el-option>
            </el-select>
            <el-select v-model="courseTime">
              <el-option
                v-for="(item, index) in times"
                :key="index"
                :label="item"
                :value="index"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时长(节)">
            <el-input-number v-model="courseLength" :min="1" :max="4"></el-input-number>
          </el-form-item>
          <el-form-item label="上课地点">
            <el-input v-model="entityForm.location"></el-input>
          </el-form-item>
          <el-form-item label="学分">
            <el-input-number v-model="entityForm.credit" :min="1" :max="10"></el-input-number>
          </el-form-item>
          <el-form-item label="最大容量">
            <el-input-number v-model="entityForm.maxSize" :min="1" :max="200"></el-input-number>
          </el-form-item>
          <el-form-item label="考试地点">
            <el-input v-model="entityForm.examLocation"></el-input>
          </el-form-item>
          <el-form-item label="课程简介">
            <el-input type="textarea" v-model="entityForm.description"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
          <el-button @click="save" type="primary">确 定</el-button>
          <el-button @click="editing = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import * as api from "../../api/teacher/course";

export default {
  name: "TeacherCourse",
  data() {
    return {
      tableData: [],
      entityForm: {},
      editing: false,
      courseDay: "",
      courseTime: "",
      courseLength: 2,
      days: [
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
        "星期日"
      ],
      times: [
        "第一节",
        "第二节",
        "第三节",
        "第四节",
        "第五节",
        "第六节",
        "第七节",
        "第八节",
        "第九节"
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    
    formatDate(date) {
      if (!date) return '';
      return new Date(date).toLocaleString();
    },getStatusType(status) {
      switch (status) {
        case 0:
          return 'info';
        case 1:
          return 'success';
        case 2:
          return 'danger';
        default:
          return 'info';
      }
    },
    getStatusText(status) {
      switch (status) {
        case 0:
          return '待审核';
        case 1:
          return '已通过';
        case 2:
          return '已拒绝';
        default:
          return '未知';
      }
    },
    getList() {
      api.getList().then(res => {
        this.tableData = res;
      });
    },
    create() {
      this.entityForm = {
        id: -1,
        name: "",
        teacherId:4,
        grade: 2019,
        time: "",
        location: "",
        credit: 2,
        maxSize: 50,
        examLocation: "",
        description: ""
      };
      this.courseDay = 1;
      this.courseTime = 1;
      this.courseLength = 2;
      this.editing = true;
    },
    edit(id) {
      api.get(id).then(res => {
        this.entityForm = res;
        // 解析上课时间
        let split = res.time.split("-");
        this.courseDay = parseInt(split[0]) - 1;
        this.courseTime = parseInt(split[1]) - 1;
        this.courseLength = parseInt(split[2]);
        this.editing = true;
      });
    },
    save() {
      // 组合上课时间
      let day = this.courseDay + 1;
      let time = this.courseTime + 1;
      this.entityForm.time = day + "-" + time + "-" + this.courseLength;
      
      console.log(this.entityForm)
      const promise = this.entityForm.id === -1 
        ? api.create(this.entityForm)
        : api.update(this.entityForm);
      promise.then(() => {
        this.$message.success(this.entityForm.id === -1 ? "创建成功" : "更新成功");
        this.editing = false;
        this.getList();
      }).catch(() => {
        this.$message.error(this.entityForm.id === -1 ? "创建失败" : "更新失败");
      });
    },
    deleteItem(id) {
      this.$confirm("确认删除该课程?", "提示", {
        type: "warning"
      }).then(() => {
        api.remove(id).then(() => {
          this.$message.success("删除成功");
          this.getList();
        });
      });
    }
  }
};
</script>

<style scoped>
.course-wrap {
  padding: 20px;
}
.container {
  margin-top: 20px;
}
.query-form {
  margin-bottom: 20px;
}
.table {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
}
.el-select {
  margin-right: 10px;
}
</style>
